﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace HQWeb.LibarySys
{
    public partial class ScrapApply : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            VerdictIsLogin();
            if (!IsPostBack)
            {
                BindBranchName();
                if (Request["id"] != null)
                {
                    hidType.Value = "edit";
                    hidPid.Value = Request["id"];
                    int scrap_id = int.Parse(Request["id"]);
                    BindDate(scrap_id);
                    if (Request["sign"] == "1")
                    {
                        allow.Style["display"] = "none";
                    }
                    else if (Request["sign"] == "2")
                    {
                        allow.Style["display"] = "";
                    }
                }
                else
                {
                    if (Request["mid"] != null)
                    {
                        BindMaterial(int.Parse(Request["mid"]));
                        applicant.Value = Session["USER_NAME"].ToString();
                    }
                    else
                    {
                        applicant.Value = Session["USER_NAME"].ToString();
                    }
                }
            }
        }

        #region 绑定分店列表
        private void BindBranchName()
        {
            BLL.SysManage.T_PUB_BRANCH LibraryBll = new BLL.SysManage.T_PUB_BRANCH();
            IList il = LibraryBll.GetModelList("");
            drop_branch.DataSource = il;
            drop_branch.DataTextField = "BRANCH_NAME"; //dropdownlist的Text的字段 
            drop_branch.DataValueField = "BRANCH_ID";//dropdownlist的Value的字段 
            drop_branch.DataBind();
            ListItem item = new ListItem("", "");
            item.Selected = true;
            drop_branch.Items.Add(item);
        }
        #endregion

        #region 绑定仓库
        private void BindLibraryName(int Branch_id)
        {
            BLL.SysManage.T_PUB_LIBRARY bll = new BLL.SysManage.T_PUB_LIBRARY();
            IList librarylist = bll.GetModelByBranch_id(Branch_id);
            if (librarylist != null)
            {
                droplibrary.DataSource = librarylist;
                droplibrary.DataTextField = "LIBRARY_NAME";
                droplibrary.DataValueField = "LIBRARY_ID";
                droplibrary.DataBind();
            }
        }
        #endregion

        #region 绑定元素
        protected void BindMaterial(int id)
        {
            Model.SysManage.T_PUB_MATERIAL model = new Model.SysManage.T_PUB_MATERIAL();
            BLL.SysManage.T_PUB_MATERIAL bll = new BLL.SysManage.T_PUB_MATERIAL();
            model = bll.GetModel(id);
            hidMaterialID.Value = model.MATERIAL_ID.ToString();
            Material_Name.Text = model.MATERIAL_NAME;
            hidTypeID.Value = model.TYPE_ID.ToString();
            hidFlag.Value = "1";
        }
        #endregion

        #region 编辑初始化数据
        protected void BindDate(int scrap_id)
        {
            Model.LibrarySys.T_PUB_SCRAP model = new Model.LibrarySys.T_PUB_SCRAP();
            BLL.LibrarySys.T_PUB_SCRAP bll = new BLL.LibrarySys.T_PUB_SCRAP();
            model = bll.GetModel(scrap_id);
            applytitel.Value = model.TITEL;
            //hidPid.Value = model.ID.ToString();
            hidLibraryID.Value = model.LIBRARY_ID.ToString();
            hidMaterialID.Value = model.MATERIAL_ID.ToString();
            applicant.Value = model.APPLICANT_NAME;
            drop_branch.SelectedValue = model.BRANCH_ID.ToString();
            BindLibraryName(int.Parse(model.BRANCH_ID.ToString()));
            droplibrary.SelectedValue = model.LIBRARY_ID.ToString();
            Material_Name.Text = model.MATERIAL_NAME;
            Materail_Num.Text = model.NUM.ToString();
            Remark.Text = model.REMARK;
            if (Request["sign"] == "2")
            {
                Approver_name.Text = model.APPLICANT_NAME;
                MyNotation.Text = model.NOTATION;
            }
        }
        #endregion

        protected void btnPass_Click(object sender, EventArgs e)
        {
            if (hidType.Value == "add")
            {
                Model.LibrarySys.T_PUB_SCRAP model = new Model.LibrarySys.T_PUB_SCRAP();
                BLL.LibrarySys.T_PUB_SCRAP bll = new BLL.LibrarySys.T_PUB_SCRAP();
                model.APPLICANT = int.Parse(Session["USER_ID"].ToString());
                model.APPLY_TIME = DateTime.Now;
                model.BRANCH_ID = int.Parse(drop_branch.SelectedValue);
                model.APPLICANT_NAME = Session["USER_NAME"].ToString();
                model.FLAG = "0";
                model.LIBRARY_ID = int.Parse(droplibrary.SelectedValue);
                model.MATERIAL_ID = int.Parse(hidMaterialID.Value);
                model.MATERIAL_NAME = Material_Name.Text;
                model.NUM = int.Parse(Materail_Num.Text);
                model.REMARK = Remark.Text.Trim();
                model.STATE = "1";
                model.TITEL = applytitel.Value.Trim();
                int flag = bll.Add(model);
                Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]), "添加报销" + model.MATERIAL_NAME + " 数量为：" + model.NUM, DateTime.Now);
                if (flag != 0)
                {
                    if (hidFlag.Value == "0")
                    {
                        Response.Redirect("ScrapList.aspx");
                    }
                    else if (hidFlag.Value == "1")
                    {
                        Response.Redirect("../Procurement/InventoryList.aspx?pid=" + hidTypeID.Value);
                    }
                }
                else if (flag == 0)
                {
                    Universal.JS.Alert("数据填写有误！申请不成功！请您重新填写！");
                }
            }
            else
            {
                int scrap_id = int.Parse(hidPid.Value);
                Model.LibrarySys.T_PUB_SCRAP model = new Model.LibrarySys.T_PUB_SCRAP();
                BLL.LibrarySys.T_PUB_SCRAP bll = new BLL.LibrarySys.T_PUB_SCRAP();
                model = bll.GetModel(scrap_id);
                model.APPLICANT = int.Parse(Session["USER_ID"].ToString());
                model.APPLY_TIME = DateTime.Now;
                model.BRANCH_ID = int.Parse(drop_branch.SelectedValue);
                model.APPLICANT_NAME = Session["USER_NAME"].ToString();
                model.FLAG = "0";
                model.LIBRARY_ID = int.Parse(droplibrary.SelectedValue);
                model.MATERIAL_ID = int.Parse(hidMaterialID.Value);
                model.MATERIAL_NAME = Material_Name.Text;
                model.NUM = int.Parse(Materail_Num.Text);
                model.REMARK = Remark.Text.Trim();
                model.STATE = "1";
                model.TITEL = applytitel.Value.Trim();
                bool flag = bll.Update(model);
                Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]), "修改报销" + model.MATERIAL_NAME + " 数量为：" + model.NUM, DateTime.Now);
                if (flag == true)
                {
                    if (hidFlag.Value == "0")
                    {
                        Response.Redirect("ScrapList.aspx");
                    }
                    else if (hidFlag.Value == "1")
                    {
                        Response.Redirect("../Procurement/InventoryList.aspx?pid=" + hidTypeID.Value);
                    }
                }
                else
                {
                    Universal.JS.Alert("数据填写有误！申请编辑不成功！请您重新填写！");
                }
            }
        }

        protected void btnReturnA_Click(object sender, EventArgs e)
        {
            if (hidFlag.Value == "0")
            {
                Response.Redirect("ScrapList.aspx");
            }
            else if (hidFlag.Value == "1")
            {
                Response.Redirect("../Procurement/InventoryList.aspx?pid=" + hidTypeID.Value);
            }
        }

        protected void drop_branch_SelectedIndexChanged(object sender, EventArgs e)
        {
            int Branch_id = int.Parse(drop_branch.SelectedValue);
            BindLibraryName(Branch_id);
        }
    }
}